package com.dengqi.mymodule.activity.umeng;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.module.umlib.alipay.RxAliPay;
import com.module.umlib.umeng.RxUmeng;
import com.module.umlib.umeng.Umeng;
import com.module.umlib.umeng.UmengFragment;
import com.module.umlib.utils.Accept;
import com.module.umlib.utils.ConstantsUtils;
import com.module.umlib.wx.RxWX;
import com.umeng.socialize.bean.SHARE_MEDIA;

import androidx.annotation.Nullable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * umeng使用
 */
public class UmengActivity extends BaseActivity {

    Disposable disposable;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_umeng);

    }

    public void onBtn1(View view) {
        initUMAuthLogin(SHARE_MEDIA.SINA);
    }

    public void onBtn2(View view) {
        initUMAuthLogin(SHARE_MEDIA.WEIXIN);
    }

    public void onBtn3(View view) {
        initUMAuthLogin(SHARE_MEDIA.QQ);
    }

    public void onBtn4(View view) {
        initUMShare(SHARE_MEDIA.SINA);
    }

    public void onBtn5(View view) {
        initUMShare(SHARE_MEDIA.WEIXIN);
    }

    public void onBtn6(View view) {
        initUMShare(SHARE_MEDIA.QQ);
    }

    public void onBtn7(View view) {
        wxPayment();
    }

    public void onBtn8(View view) {
        wxAuthLogin();
    }

    public void onBtn9(View view) {
        aliPayment();
    }


    /**
     * 第三方登录
     */
    private void initUMAuthLogin(SHARE_MEDIA shareMedia) {
        if (disposable != null) {
            disposable.dispose();
        }
        disposable = RxUmeng.with(this)
                .umAuthLogin(shareMedia)
                .subscribe(new Consumer<Umeng>() {
                    @Override
                    public void accept(Umeng umeng) throws Exception {
                        if (umeng.platform.equals(SHARE_MEDIA.QQ)) {
                            Toast.makeText(UmengActivity.this, umeng.getAccessTokenBySina(), Toast.LENGTH_SHORT).show();
                        } else if (umeng.platform.equals(SHARE_MEDIA.WEIXIN)) {
                            Toast.makeText(UmengActivity.this, umeng.getAccessTokenBySina(), Toast.LENGTH_SHORT).show();
                        } else if (umeng.platform.equals(SHARE_MEDIA.SINA)) {
                            Toast.makeText(UmengActivity.this, umeng.getAccessTokenBySina(), Toast.LENGTH_SHORT).show();
                        }
                        Log.e("---", umeng.data.toString());
                    }
                });
    }

    private void initUMShare(SHARE_MEDIA shareMedia) {
        if (disposable != null) {
            disposable.dispose();
        }
        disposable = RxUmeng.with(this)
                .umShare(shareMedia, "http://www.baidu.com", "", "", "")
                .subscribe(new Consumer<Umeng>() {
                    @Override
                    public void accept(Umeng umeng) throws Exception {
                        Toast.makeText(UmengActivity.this, umeng.getPlatformName() + "分享成功", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void wxPayment() {
        if (disposable != null) {
            disposable.dispose();
        }
        disposable = RxWX.with(this)
                .wxPayment(null)
                .subscribe(new Consumer<Accept>() {
                    @Override
                    public void accept(Accept accept) throws Exception {
//                        if(accept.status == AcceptStatus.Complete){
//                            Toast.makeText(UmengActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
//                        }
                    }
                });
    }

    private void wxAuthLogin() {
        if (disposable != null) {
            disposable.dispose();
        }
        disposable = RxWX.with(this)
                .wxAuthLogin()
                .subscribe(new Consumer<Accept>() {
                    @Override
                    public void accept(Accept accept) throws Exception {
//                        if(accept.status == AcceptStatus.Complete){
//                            Toast.makeText(UmengActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                        }
                    }
                });
    }

    private void aliPayment() {
        if (disposable != null) {
            disposable.dispose();
        }
        disposable = RxAliPay.with(this)
                .aliPayment("123123123123123")
                .subscribe(new Consumer<Accept>() {
                    @Override
                    public void accept(Accept accept) throws Exception {
                        if(accept.status == ConstantsUtils.Complete){
                            Toast.makeText(UmengActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 坑：只能回调到activity的onActivityResult，只好activity中加上 UmengFragment.newInstance(this).onActivityResult(requestCode, resultCode, data);
         * 只有QQ
         */
        UmengFragment.newInstance(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
        super.onDestroy();
    }
}
